﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using GCELibrary.Current;
namespace ZF.WS.expert
{
    public partial class list :BLL.Page.PageBase
    {
        [PageArgument(Key = "type", DefaultValue = 0)]
        public int TypeId { get; set; }
        [PageArgument(Key = "pi", DefaultValue = 0)]
        public int Pi { get; set; }

        [PageArgument(Key = "tn", DefaultValue = null)]
        public string tn
        {
            get;
            set;
        }

        [PageArgument(Key = "name", DefaultValue = null)]
        public string Account { get; set; }

        [PageArgument(Key = "company", DefaultValue = null)]
        public string Company { get; set; }
        [PageArgument(Key = "tel", DefaultValue = null)]
        public string Tel { get; set; }
        [PageArgument(Key = "area", DefaultValue = null)]
        public string Area { get; set; }
        protected string _param;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (tn != null)
            {
                var tt = DictionaryConfig.EExpertType.FirstOrDefault(p => p.Text == tn);
                if (tt != null)
                {

                    this.TypeId = tt.Key;
                }
            }

            var typeList = DictionaryConfig.EExpertType;
            this.lvTitle.DataSource = typeList;
            this.lvTitle.DataBind();
 
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                string param = "?";
                foreach (var name in Request.QueryString.AllKeys)
                {
                    if (name != null && !name.EqIgnoreCase("pi"))
                    {
                        param += "&" + name + "=" + Request.QueryString[name];
                    }
                }

                _param = "/expert/list.aspx" + param;


                var sel = dc.US_Member.Where(p => p.IsExpert);
                if (this.TypeId != 0)
                    sel = sel.Where(p => p.ExpertType == this.TypeId);
    
                if (!string.IsNullOrEmpty(Area)){
                    Area = Area.Replace("市", "");
                    sel = sel.Where(p => p.Area.Contains(Area));
                }
                if (!string.IsNullOrEmpty(Account))
                    sel = sel.Where(p => p.Account.Contains(Account));
                if (!string.IsNullOrEmpty(Company))
                    sel = sel.Where(p => p.Company.Contains(Company));
                if (!string.IsNullOrEmpty(Tel))
                    sel = sel.Where(p => p.Tel.Contains(Tel) || p.Phone.Contains(Tel));
  

                var pagesize = 10;
     
                var rowcount = sel.OrderByDescending(p => p.Hits).Count();
                if (rowcount > 0)
                {
                    var pagecount = rowcount / pagesize;
                    if (rowcount % pagesize != 0)
                        pagecount++;
                    if (Pi >= pagecount)
                        Pi = pagecount - 1;
                    var list = sel.OrderByDescending(p => p.Hits).Skip(Pi * pagesize)
                            .Take(pagesize).ToList().Select(p => new
                            {
                     
                                Member = p,
                                EType = DictionaryConfig.EExpertType.FirstOrDefault(t => t.Key == p.ExpertType).Text
                            }).ToList();
                    this.lvList.DataSource = list;
                    this.lvList.DataBind();
                }
                var easel = dc.E_Article.Where(p=>1==1);
                if (this.TypeId != 0)
                    easel = easel.Where(p => p.Type == this.TypeId);
                var artList = easel.OrderByDescending(p => p.Hits).ToList();
 

                this.lvArticle.DataSource = artList.Take(20);
                this.lvArticle.DataBind();

           

            }
        }

        public string _GetType(int id)
        {
            var arr = new string[] { "", "文章案例", "文章案例", "推荐房源", "贷款种类" };

            return arr[id];

        }

    }
}
